<template>
  <el-row style="width: 100%" :gutter="10" type="flex" justify="space-between">
    <el-col :span="6">{{ treeNode.name }}</el-col>
    <el-col :span="6">
      <el-row type="flex">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown class="drop" @command="handleClick">
            <span class="text">
              操作<i class="el-icon-arrow-down" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">
                  添加部门
                </el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="edit">
                  编辑部门
                </el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">
                  删除部门
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 组件之间的数据共享
import { delDepartments } from '@/api/departs'

export default {
  name: 'TreeTool',
  props: {
    treeNode: Object,
    // 是否为根节点
    isRoot: Boolean
  },
  data: function() {
    return {}
  },
  methods: {
    async handleClick(type) {
      if (type === 'add') {
        this.$emit('addDept', this.treeNode)
      } else if (type === 'edit') {
        this.$emit('editDept', this.treeNode)
      } else if (type === 'del') {
        await this.$confirm('是否确认删除')
        await delDepartments(this.treeNode.id)
        this.$message.success('删除成功')
        this.$emit('getDeparts')
      }
    }
  }
}
// options.$data = typeof options.data === 'function' ? options.data() : options.data
</script>

<style scoped>

</style>
